# _*_ coding: utf-8 _*_
from odoo import models, fields, api
from odoo.exceptions import UserError


class StockPicking(models.Model):
    _inherit = "stock.picking"

    is_todo = fields.Boolean(u"是待办", copy=False)

    def action_submit_todo(self):
        self.ensure_one()
        if self.state not in ('assigned', 'partially_available'):
            raise UserError(u"只有可用状态、部分可用状态的调拨单才能标记为待办。")
        self.is_todo = True

    def action_cancel_todo(self):
        self.ensure_one()
        self.is_todo = False

    @api.model
    def create(self, vals):
        picking_type_id = vals.get("picking_type_id")
        if picking_type_id:
            picking_type = self.env["stock.picking.type"].browse(picking_type_id)
            if not picking_type.enable_todo:
                vals["is_todo"] = True
        res = super(StockPicking, self).create(vals)
        return res
